package cn.edu.zzuli.wechat.user.controller;


import cn.edu.zzuli.wechat.filter.NoAuth;
import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.model.entity.OfferOrder;
import cn.edu.zzuli.model.vo.order.AllOfferOrderQueryVo;
import cn.edu.zzuli.model.vo.order.ConditionQueryVo;
import cn.edu.zzuli.model.vo.all.SelectQuery;
import cn.edu.zzuli.wechat.user.service.OfferOrderService;
import cn.edu.zzuli.wechat.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "订单相关接口")
public class OfferOrderController {

    @Autowired
    private OfferOrderService offerOrderService;

    @Autowired
    private UserService userService;


    @ApiOperation("发布订单")
    @GetMapping("/addOrder.do")
    public ResultInfo addOfferOrder(OfferOrder order, HttpSession session){
        int res = offerOrderService.addOfferOrder(order,session);
        return Result.ok();
    }


    @ApiOperation("所有用户的订单列表")
    @GetMapping("/queryOferOrder.do")
    public Map<String, Object> queryOferOrder(AllOfferOrderQueryVo queryVo){
        Map<String, Object> map = offerOrderService.queryOfferOrderList(queryVo);
        return map;
    }


    @ApiOperation("通过条件查询所有用户订单列表")
    @GetMapping("/searchByType.do")
    public Map<String, Object> searchByType(AllOfferOrderQueryVo allOfferOrderQueryVo){
        Map<String, Object> map = offerOrderService.queryOfferList(allOfferOrderQueryVo);
        return map;
    }


    @ApiOperation("跑单数量排行")
    @GetMapping("/rankByCount.do")
    @NoAuth
    public Map<String, Object> rankByCount(SelectQuery selectQuery){
        Map<String, Object> map = offerOrderService.queryByOrderCount(selectQuery);
        return map;
    }

    @ApiOperation("通过状态查询自己发布的订单")
    @GetMapping("/queryOwnOrder.do")
    public Map<String, Object> queryOwnOrder(SelectQuery selectQuery,Integer status,HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        Map<String, Object> map = offerOrderService.queryOfferOwnOrder(selectQuery,status,uid);
        return map;
    }

    @ApiOperation("多条件筛选用户订单")
    @PostMapping("conditions.do")
    public ResultInfo conditions(@RequestBody ConditionQueryVo conditionQueryVo,SelectQuery selectQuery){
        System.out.println(conditionQueryVo);
        Map<String, Object> map = offerOrderService.choose(conditionQueryVo,selectQuery);
        return Result.ok(map);
    }

    @ApiOperation("取消一个发布的订单")
    @GetMapping("/delOfferOrder.do")
    public ResultInfo delOfferOrder(int id,Double bounty,HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        offerOrderService.removeById(id);
        //积分恢复
        userService.updateWealth(uid,bounty);
        return Result.ok();
    }

    @ApiOperation("订单确认")
    @GetMapping("/sureFinish.do")
    public ResultInfo sureFinish(Integer id, Integer ruid, Double bounty,String finishTime
                                ,HttpSession session){

        Integer uid = (Integer) session.getAttribute("uid");
        offerOrderService.sureFinish(id,finishTime,uid);

        //增加积分
        userService.updateWealth(ruid,bounty);
        return Result.ok();
    }

    @NoAuth
    @ApiOperation("发布订单")
    @PostMapping("/editOrder/{oid}")
    public ResultInfo editOrder(@RequestBody OfferOrder order, @PathVariable(name = "oid")Integer oid){
        order.setOid(oid);
        offerOrderService.edit(order);
        return Result.ok();
    }
}
